﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NHibernate;

namespace Cms.BLL
{
    public class UserInfo:BaseDao<Model.Sys_Userinfo,string>
    {
        public bool ValidPassword(string username,string pass) {
            bool result = false;
            List<ParamInfo> paramInfos = new List<ParamInfo>();
            paramInfos.Add(new ParamInfo("al", username, NHibernateUtil.String));
            IList list = this.Find(String.Format("select u.Password from Sys_Userinfo u where u.Alias=:al"), paramInfos);
            if (list!=null && list.Count > 0)
            {
                object p = list[0];
                //加密密码
                pass = EntLib.Utiltiy.MD5Encrypt(pass);
                if (pass.Equals(p)) {
                    result = true;
                }
            }
            return result;
        }
        public Model.Sys_Userinfo GetByUserName(string username) {
            Model.Sys_Userinfo result = null;
            List<ParamInfo> paramInfos = new List<ParamInfo>();
            paramInfos.Add(new ParamInfo("al", username, NHibernateUtil.String));
            IList<Model.Sys_Userinfo> list = this.Find<Model.Sys_Userinfo>(String.Format("from Sys_Userinfo u where u.Alias=:al"), paramInfos);
            if (list != null && list.Count > 0)
            {
                result = list[0];
            }
            return result; 
        }
        //public 
        public Model.Sys_Userinfo GetById(string id, bool shouldLock)
        {
            Model.Sys_Userinfo model= base.GetById(id, shouldLock);
            model.Password = EntLib.Utiltiy.MD5Decrypt(model.Password);
            return model;
        }
        public Model.Sys_Userinfo Save(Model.Sys_Userinfo entity)
        {
            entity.Password = EntLib.Utiltiy.MD5Encrypt(entity.Password);
            return base.Save(entity);
        }
        public Model.Sys_Userinfo SaveOrUpdate(Model.Sys_Userinfo entity)
        {
            entity.Password = EntLib.Utiltiy.MD5Encrypt(entity.Password);
            return base.SaveOrUpdate(entity);
        }
    }
}
